<?php

define('EXECTIME', microtime(true));

require_once('../init.' . pathinfo(__FILE__, PATHINFO_EXTENSION));

require_once './Loader' . EXT;

$loader = new Loader;

if(!$loader->session_is_valid())
{
	$rules = array(
		'username' => array(
			'required' => 1,
			'title' => 'Username'
		),
		'password' => array(
			'required' => 1,
			'title' => 'Password'
		)
	);
	if(!$loader->base->lib->validation($rules))
	{
		$loader->display->login();
	}
	else
	{
		$auth = array(
			'username' => $loader->base->lib->input_post('username'),
		);
		$loader->base->db->select()->from('users')->where('username', $auth['username'])->run();
		if($loader->base->db->num_rows() > 0) {
			$user = $loader->base->db->row();
			//$permissions = User::get_permissions($user->id);
			$auth['password'] = $loader->base->lib->user_hash($loader->base->lib->input_post('password'), $user['key']);
			$auth['key'] = $user['key'];
			$loader->base->db->select()->from('users')->where($auth)->run();
			if($loader->base->db->num_rows() > 0 /*&& $permissions['can_access_acp']*/)
			{
				$user_id = $loader->base->db->row('id');
				$ipaddress = substr($loader->base->lib->input_server('REMOTE_ADDR'), 0, 255);
				$useragent = substr($loader->base->lib->input_server('HTTP_USER_AGENT'), 0, 255);
				$session = array(
					'sid' => md5($ipaddress . $useragent . microtime(true) . rand(1, 500)),
					'user_id' => $user_id,
					'address' => $ipaddress,
					'useragent' => $useragent,
					'location' => '/index.php',
					'access_time' => $loader->base->lib->timestamp()
					
				);
				$loader->base->db->insert('dashboard_sessions', $session);
				$loader->setcookie('super_session', $session['sid'], $loader->base->lib->timestamp() + (60*60*24*100));
				//$loader->log_login_attempt(Input::post('username'), 1);
				//$loader->redirect_browser($loader->base->db->dashboard . 'index.php', 2, 'Authentication Successful, please wait while you are transferred.');
			}
			else
			{
				echo 'Invalid Pass';
				//$loader->log_login_attempt(Input::post('username'));
				//Error::out(Lang::get('l_error_login'));
			}
		}
		else
		{
			echo 'Invalid User';
			//$loader->log_login_attempt($_POST('username'));
			//Error::out(Lang::get('l_error_login'));
		}
	}
}
else
{
	$ajax = new Ajax;
	$ajax->load = &$loader;
	$ajax->main();
	
	$menu = array(
		'Settings' => array(
			'uri' => array('com' => 'settings'),
			'System Settings' => array('com' => 'settings', 'section' => 'system'),
			'Security Settings' => array('com' => 'settings', 'section' => 'security'),
			'Administration Policies' => array('com' => 'advanced', 'section' => 'policies'),

		),
		'Forum Management' => array(
			'uri' => array(),
			'Manage Forums' => array('com' => 'forums', 'section' => 'list'),
			'Warning System' => array('com' => 'moderation', 'section' => 'warnings'),
			'Configure Trashcan' => array('com' => 'forums', 'section' => 'trashcan'),
			'Quick Moderation' => array('com' => 'moderation', 'section' => 'quickmod'),
			'One ClickMod Configuration' => array('com' => 'moderation', 'section' => 'oneclick'),
		),
		'Groups & Permissions' => array(
			'uri' => array(),
			'Manage Permission masks' => array('com' => 'permissions', 'section' => 'list'),
			'Manage Groups' => array('com' => 'groups', 'section' => 'list'),
			'Edit Usertitles' => array('com' => 'groups', 'section' => 'usertitles'),
		),
		'Tools & Tasks' => array(
			'uri' => array(),
			'AutoPrune' => array('com' => 'tasks', 'section' => 'autoprune'),
			'Schedule AutoPosts' => array('com' => 'tasks', 'section' => 'autoposts'),
			'Database Utilities' => array('com' => 'tools', 'section' => 'db'),
			'The Blackhole' => array('com' => 'tools', 'section' => 'blackhole'),
			'Update Statistics' => array('com' => 'tasks', 'section' => 'refreshstats'),
			'Cleanup & Optimisation' => array('tools' => 'cleanup'),
		),
		'Logs' => array(
			'uri' => array(),
			'Administration Logs' => array('com' => 'logs', 'section' => 'admin'),
			'Dashboard login attempts' => array('com' => 'logs', 'section' => 'login'),
			'Moderation Logs' => array('com' => 'logs', 'section' => 'moderation'),
			'Error Logs' => array('com' => 'logs', 'section' => 'errors'),
		),
	);
	if(isset($_GET['com']) && $loader->valid_query($_GET['com']) && file_exists('./controllers/' . $_GET['com'] . EXT))
	{ 
		require './controllers/' . $_GET['com'] . EXT;
		
		$loader->display->top();
		
		$controller_name = ucfirst(strtolower($_GET['com']));
		$controller = new $controller_name;
		$controller->load = &$loader;
		if(isset($_GET['section']) && $loader->valid_query($_GET['section']) && method_exists($controller, $_GET['section']))
		{
			$method = $_GET['section'];
			$controller->$method();
		}
		else
		{
			if(method_exists($controller, 'main'))
			{
				$controller->main();
			}
			
		}
	}
	else
	{
		
		require './controllers/frontend' . EXT;
		$controller = new Frontend;
		$controller->load = &$loader;
		
		$loader->display->top();
		$controller->main();
	}
	
	$loader->display->sidebar($menu);
	$loader->display->bottom();
}

?>